/**
 * 
 */
package com.xohm.platform.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

/**
 * Xohm Connection Manager
 *
 * Licensed Material - Property of Sprint Nextel
 * Restricted Materials of Sprint Nextel
 * (C) Copyright Sprint Nextel Corp. 2008 - All Rights Reserved.
 *
 * <Class Description>
 *
 * @author rdk3927
 */
public abstract class OSAPIPidLookup extends OSAPIBase
{
	private String pidFileName = "cm.pid";

	public OSAPIPidLookup()
	{
	}

	
	/**
	 * This method will check for an existing Connection Manager process.  If
	 * there is one then kill this one and bring up the existing one, otherwise
	 * save this PID and continue bringing up this GUI.
	 * 
	 * @return boolean
	 */
	public boolean doesProcessExist()
	{
		boolean processFound = false;
		
		// Get process id from pid file
		int pid = readPidFile(pidFileName);
		
		// Check to see if it's running
		boolean processExists = false;
		if (pid > -1)
			processExists = isProcessRunning(pid);
		
		if (processExists)
		{
			processFound = true;
		}
		else
		{
			// else save this process id in the local pid file and continue to bring up the window
			savePid(getProcessId());
		}
		
		return processFound;
	}

	/**
	 * This method will this processes PID to a local file.
	 * 
	 * @param pid int process id to save
	 */
	protected void savePid(int pid)
	{
		java.net.URL url = ClassLoader.getSystemResource(pidFileName);
		if (url != null)
		{
			writeToFile(url.getPath(), String.valueOf(pid));
		}else {
			try {
				File pidFile = new File(pidFileName);
				pidFile.createNewFile();
				writeToFile(pidFile.getPath(), String.valueOf(pid));
			}catch(Exception ioEx) {
				//Failed to create the pid file.
			}
		}
	}
	
	/**
	 * This method will an object to the file system, the object must
	 * be serializable.
	 * 
	 * @param fileLoc java.lang.String file name and location
	 * @param value java.lang.Object serializable object to be written to the file system.
	 */
	protected void writeToFile(String fileLoc, Object value)
	{
		FileOutputStream fileOutStream   = null;
		ObjectOutputStream dataOutStream = null;

		try
		{
			String file = java.net.URLDecoder.decode(fileLoc, "UTF-8");
			
			fileOutStream = new FileOutputStream(file);
			dataOutStream = new ObjectOutputStream(fileOutStream);

			dataOutStream.writeObject(String.valueOf(value));

			fileOutStream.close();
			dataOutStream.close();
		}
		catch (Exception e)
		{
			try
			{
				if (fileOutStream != null) fileOutStream.close();
				if (dataOutStream != null) dataOutStream.close();
			} catch (Exception e2) {}
		}
	}
	
	/**
	 * This method will read an object file and return it to the receiver.
	 * 
	 * @param fileLoc String file name and location
	 * @return java.lang.Object read
	 */
	protected Object readFromFile(String fileLoc)
	{
		Object obj = null;

		FileInputStream fileInStream = null;
		ObjectInputStream dataInStream = null;
		try
		{
			String file = java.net.URLDecoder.decode(fileLoc, "UTF-8");

			fileInStream = new FileInputStream(file);
			dataInStream = new ObjectInputStream(fileInStream);

			obj = dataInStream.readObject();

			fileInStream.close();
			dataInStream.close();
		}
		catch (Exception e)
		{
			try
			{
				if (fileInStream != null) fileInStream.close();
				if (dataInStream != null) dataInStream.close();
			} catch (Exception e2) {}
		}

		return obj;
	}

	/**
	 * This method will clear the pid present in the pid file.
	 * 
	 */
	protected void clearPidFile()
	{
		java.net.URL url = ClassLoader.getSystemResource(pidFileName);
		if (url != null)
		{
			String fileLoc = url.getFile();
			writeToFile(fileLoc, "-1");
		}
	}
	
	/**
	 * This method will locate the pid file and read the process id.  If the file
	 * does not exist in the class path then it will return a -1 for pid not found.
	 * 
	 * @param pidFileName String pid file name to find in the classpath
	 * @return int pid
	 */
	protected int readPidFile(String pidFileName)
	{
		int pid = -1;
		
		java.net.URL url = ClassLoader.getSystemResource(pidFileName);
		if (url != null)
		{
			Object obj = readFromFile(url.getFile());
			String pidString = null;
			if (obj != null)
				pidString = (String)obj;

			if (pidString != null && !pidString.trim().equals(""))
				pid = Integer.valueOf(pidString).intValue();
		}
		
		return pid;
	}
	
	abstract Vector<DriverInformation> getAvailableDriverInformation();
	abstract boolean isProcessRunning(int pidToFind);
	abstract int getProcessId();
	abstract void openUrl(String url);
}
